﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.NemoBlog.Business;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using blackhouse.mojoPortalExtend;

namespace blackhouse.NemoBlog.Web {
    public class PostUrlSupport : UrlSupport {

        #region Fields

        private NemoBlogPost post;
        private int urlVariantIdx = 0;
        private PageSettings newsPage;

        #endregion

        #region Constructors

        public PostUrlSupport(NemoBlogPost post) {
            this.post = post;
            this.newsPage = this.GetNewsPage();
            this.OldUrl = this.post.PostUrl;
            this.NewUrl = this.GetNewUrl(this.urlVariantIdx);
            this.RealUrl = "~/NemoBlogPost.aspx?post=" + this.post.PostId + "&pageId=" + this.newsPage.PageId;
            this.Guid = this.post.PostGuid;
        }

        #endregion

        #region Methods

        private PageSettings GetNewsPage() {
            PageSettings page = null;
            page = this.GetNewsPageFromQueryString();
            if (page != null) return page;

            page = this.GetNewsPageFromModuleId();
            if (page != null) return page;

            page = this.GetNewsPageFromPageKey();
            if (page != null) return page;

            return CacheHelper.GetCurrentPage();
        }

        private PageSettings GetNewsPageFromQueryString() {
            if (HttpContext.Current == null || HttpContext.Current.Request == null) return null;
            string spage = HttpContext.Current.Request["pageid"];
            int pageid = 0;
            if (Int32.TryParse(spage, out pageid))
                return CacheHelper.GetPage(pageid);
            else
                return null;

        }

        private PageSettings GetNewsPageFromModuleId() {
            List<PageModule> pageModules = PageModule.GetPageModulesByModule(this.post.ModuleId);
            if (pageModules.Count > 0)
                return CacheHelper.GetPage(pageModules.First().PageId);
            else
                return null;
        }

        private PageSettings GetNewsPageFromPageKey() {
            PageProvider pp = new PageProvider();
            return pp.FindCmsPageByFunctionality("news");
        }

        public override void Save() {
            this.post.PostUrl = this.NewUrl;
            this.post.Save(false);
        }

        public override void ChangeToOtherNewUrl() {
            if (this.NewUrl.Equals(GetNewUrl(this.urlVariantIdx), StringComparison.InvariantCultureIgnoreCase))
                this.urlVariantIdx++;
            this.NewUrl = GetNewUrl(this.urlVariantIdx);
        }

        private string GetNewUrl(int variantIdx) {
            switch (variantIdx) {
                case 0:
                    return "~/" + this.post.Title.RemovePolishChars().CleanStringForUrl();
                case 1:
                    return "~/post-" + this.post.Title.RemovePolishChars().CleanStringForUrl();
                case 2:
                    DateTime dt = this.post.CreatedTime;
                    if (this.post.IsPublished)
                        dt = this.post.PublishDate;
                    return "~/" + this.post.Title.RemovePolishChars().CleanStringForUrl() + "-" + dt.ToString("dd-MM-yyyy");
                default:
                    throw new UrlInUseException(this);
            }
        }

        public void SetUserNewUrl(string url) {
            this.NewUrl = url;
        }

        #endregion

    }
}